package com.zy.oss.controller;


import com.zy.commont.result.Result;
import com.zy.oss.service.FileService;
import com.zy.oss.util.ConstantPropertiesUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/*
文件上传的服务
 */
@RestController
@RequestMapping("oss")
@CrossOrigin //跨域请求
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传文件头像
     * 接收请求
     * 返回响应
     */
    @PostMapping("file/upload")
    public Result upload(@ApiParam(name = "file", value = "文件", required = true)
                         @RequestParam("file") MultipartFile file,
                         @ApiParam(name = "host", value = "文件上传路径", required = false)
                         @RequestParam("host") String host) {
        if (!StringUtils.isEmpty(host)) { //cover（前端传）--->上传文件路径有就传，没有就是默认值(配置文件)test
            ConstantPropertiesUtil.FILE_HOST = host;
        }
        String url = fileService.upload(file);
        System.out.println("url======" + url);
        return Result.ok().data("url", url);
    }

}
